<template>
  <div>
    <el-card>
      <el-form :inline="true" :model="searchForm">
        <el-form-item label="账号">
          <el-input
            v-model="searchForm.accountNumber"
            placeholder="请输入"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input
            v-model="searchForm.name"
            placeholder="请输入"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getData()">查询</el-button>
          <el-button type="primary" v-if="searchForm.accountNumber">
          <a style="color:white" :href="'http://localhost:8081/VehicleManagement/user/exportUserInfoList?accountNumber=' + searchForm.accountNumber">导出</a>
          </el-button>
          <el-button type="primary" v-else>
          <a style="color:white" href="http://localhost:8081/VehicleManagement/user/exportUserInfoList">导出</a>
          </el-button>
        </el-form-item>
      </el-form>
      <!-- 数据列表-->
      <el-table
        :data="tableData.slice((currentPage-1)*PageSize,currentPage*PageSize)"
        border
        class="table"
        ref="multipleTable"
        highlight-current-row
      >
        <el-table-column
          prop="accountNumber"
          label="账号"
          min-width="140"
        ></el-table-column>
        <el-table-column
          prop="name"
          label="姓名"
          min-width="140"
        ></el-table-column>
        <el-table-column prop="type" label="身份" min-width="140">
          <template slot-scope="scope">
            <span v-if="scope.row.type === '0'">管理员</span>
            <span v-if="scope.row.type === '1'">业主</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="carNumber"
          label="车牌号"
          min-width="140"
        ></el-table-column>
        <el-table-column
          prop="phone"
          label="手机号"
          min-width="140"
        ></el-table-column>
        <el-table-column prop="gender" label="性别" min-width="50">
          <template slot-scope="scope">
            <span v-if="scope.row.gender === '0'">男</span>
            <span v-if="scope.row.gender === '1'">女</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="birthday"
          label="出生年月日"
          min-width="100"
        ></el-table-column>
        <el-table-column label="住址" min-width="150">
          <!-- 使用作用域插槽，可以获取这一行返回的数据 -->
          <template slot-scope="scope">
            {{ scope.row.buildingNumber }}号楼-{{ scope.row.unitNumber }}单元-{{
              scope.row.houseNumber
            }}
          </template>
        </el-table-column>

        <el-table-column
          label="操作"
          min-width="150"
          align="center"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="openDialog('编辑人员信息', scope.row)"
              style="margin-right:5px"
              >编辑
            </el-button>
            <el-popconfirm
                confirm-button-text='确定'
                cancel-button-text='取消'
                icon="el-icon-info"
                @confirm="deleteUser(scope.row)"
                icon-color="red"
                title="确定删除该人员信息吗？"
                >
                <el-button
              type="text"
              slot="reference"
              icon="el-icon-delete"
              class="red"
              >删除
            </el-button>
            </el-popconfirm>
            
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="pageSizes"
          :page-size="PageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.length"
        >
        </el-pagination>
      </div>
      <!--添加/修改 模态框-->
      <el-dialog
        :title="dialogTitle"
        :visible.sync="dialogFormVisible"
        width="500px"
      >
        <el-form
          :model="editUserinfoForm"
          ref="editUserinfoForm"
          :rules="userInfoRules"
          label-width="120px"
          :inline="true"
        >
          <el-form-item label="姓名:">
            <el-input v-model="editUserinfoForm.name" ></el-input>
          </el-form-item>
          <el-form-item  label="身份:">
            <el-select v-model="editUserinfoForm.type" >
              <el-option label="管理员" value="0"> </el-option>
              <el-option label="业主" value="1"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="性别:">
             <el-radio v-model="editUserinfoForm.gender" :label=0
            >男</el-radio
          >
          <el-radio v-model="editUserinfoForm.gender" :label=1
            >女</el-radio
          >
          </el-form-item>
          <el-form-item label="手机号:">
            <el-input v-model="editUserinfoForm.phone" ></el-input>
          </el-form-item>
          <el-form-item label="身份证号:">
            <el-input v-model="editUserinfoForm.idCard" ></el-input>
          </el-form-item>
          <el-form-item label="楼号" prop="buildingNumber">
            <el-input
              v-model="editUserinfoForm.buildingNumber"
            ></el-input>
          </el-form-item>
          <el-form-item label="单元号" prop="unitNumber">
            <el-input v-model="editUserinfoForm.unitNumber" ></el-input>
          </el-form-item>
          <el-form-item label="户号" prop="houseNumber">
            <el-input v-model="editUserinfoForm.houseNumber" ></el-input>
          </el-form-item>
          <el-form-item label="出生年月日:">
            <el-date-picker
              v-model="editUserinfoForm.birthday"
              type="date"
              placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button
            type="primary"
            @click="submit"
            v-if="dialogTitle === '添加人员信息'"
            >提 交</el-button
          >
          <el-button
            type="primary"
            @click="saveEdit"
            v-if="dialogTitle === '编辑人员信息'"
            >保 存</el-button
          >
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
import webservice from "../../utils/webservice";
export default {
  data() {
      //校验手机号
        var validatephone = (rule, value, callback) => {
        var reg = /^1[3|4|5|7|8][0-9]{9}$/; //验证规则
        var flag = reg.test(value); //true
        setTimeout(() => {
          if (value === '') {
            callback(new Error('请输入手机号'));
          } else {
            if (!flag) {
              callback(new Error('请输入正确格式的手机号'));
            } else {
              callback();
            }
          }
        }, 100);
      };
      //校验身份证号
        var validateidCard = (rule, value, callback) => {
        // 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X
        var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
        var flag = reg.test(value); //true
        setTimeout(() => {
          if (value === '') {
            callback(new Error('请输入身份证号'));
          } else {
            if (!flag) {
              callback(new Error('请输入正确格式的身份证号'));
            } else {
              callback();
            }
          }
        }, 100);
      };
    return {
      tableData: [],
      // 默认显示第几页
        currentPage: 1,
        // 个数选择器（可修改）
        pageSizes: [1, 5, 10, 20],
        // 默认每页显示的条数（可修改）
        PageSize: 5,
      searchForm: {}, //查询对象
      dialogFormVisible: false, //添加或修改的模态框标识
      dialogTitle: "",
      editUserinfoForm: {}, //编辑人员信息的表单信息
      userInfoRules:{
          name: [
                    { required: true, message: '请输入姓名', trigger: 'blur' },
                 ],
                 phone: [
                     { validator: validatephone, trigger: 'blur' }
                 ],
                 idCard: [
                     { validator: validateidCard, trigger: 'blur' }
                 ],
                 type: [
                     { required: true, message: '请选择您的身份', trigger: 'blur' }
                 ],
      }
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    // 分页
    // 每页显示的条数
    handleSizeChange(val) {
        // 改变每页显示的条数 
        this.PageSize = val
        console.log(this.pageSizes)
        // 注意：在改变每页显示的条数时，要将页码显示到第一页
        this.currentPage = 1
    },
    // 显示第几页
    handleCurrentChange(val) {
        // 改变默认的页数
        this.currentPage = val
    },
    getData() {
      let _param = {
        accountNumber: this.searchForm.accountNumber,
        name: this.searchForm.name,
      };
      webservice.HttpPost("/api/user/findUserInfoList", _param, (data) => {
        if (data.code === 200) {
          this.tableData = data.data;
        } else {
          this.$message.error({
            message: data.message,
            showClose: true,
            duration: 1000,
          });
        }
      });
    },
    //打开 添加/编辑 模态框
    openDialog(flag, row) {
      this.dialogTitle = flag;
      this.editUserinfoForm = JSON.parse(JSON.stringify(row));
      this.dialogFormVisible = true;
    },
    //修改人员信息
    saveEdit() {
      let _param = {
        id:this.editUserinfoForm.id,
        name:this.editUserinfoForm.name,
        type:this.editUserinfoForm.type,
        gender:this.editUserinfoForm.gender,
        phone:this.editUserinfoForm.phone,
        idCard:this.editUserinfoForm.idCard,
        birthday:this.editUserinfoForm.birthday,
        buildingNumber:this.editUserinfoForm.buildingNumber,
        unitNumber:this.editUserinfoForm.unitNumber,
        houseNumber:this.editUserinfoForm.houseNumber,
      };
      webservice.HttpPost("/api/user/updateUserInfo", _param, (data) => {
        if (data.code === 200) {
          this.dialogFormVisible = false;
          this.getData()
          this.$message.success({
            message: '修改成功！',
            showClose: true,
            duration: 1000,
          });
        } else {
          this.dialogFormVisible = false;
          this.$message.error({
            message: data.message,
            showClose: true,
            duration: 1000,
          });
        }
      });
    },
    //删除人员信息
    deleteUser(row){
      let _param = {
        id: row.id
      };
      webservice.HttpPost("/api/user/deleteUserInfo", _param, (data) => {
        if (data.code === 200) {
            this.getData()
            this.$message.success({
            message: '删除成功！',
            showClose: true,
            duration: 1000,
          });
        } else {
          this.$message.error({
            message: data.message,
            showClose: true,
            duration: 1000,
          });
        }
      });
    },
  },
};
</script>